/**
 * xxx 模型
 */

'use strict';

var mongodb = require('../mongodb.js');
var Schema = mongodb.Schema;
var ObjectId = Schema.Types.ObjectId;

// create a schema
var ModelSchema = new Schema({
    name: String,
    shopId: ObjectId,
    description: String,

    // 地区 & loc
    districts: [{_id: ObjectId, pId: ObjectId, name: String}],
    loc: {
        'type': {type: String, 'enum': "Point", 'default': "Point"},
        coordinates: {type: [Number], 'default': [0, 0]}
    },
    // ---种类
    cates: [{type: String, ref: 'category'}],

    //
    createdAt: Date,
    updatedAt: {type: Date, 'default': Date.now()},

    imgUrl: [String], // ---图片url
    webUrl: String,
    status: String, // 未上架,已上架,已下架...
    price: Number, // price
    priceMarket: Number,
    amountTotal: Number, // mount
    amountRemain: Number,
    grade: Number, // 商品评分

    // 实际上 ware 也是树状接口,因为后续的产品,可能沿用之前的产品的部分特性(评论,消费人数)
    // 这里不用id进行排序,使用version
    version: Number,
    preVer: [{
        _id: {type: ObjectId, ref: 'ware'},
        version: Number
    }]
});

ModelSchema.pre('save', function (next) {
    if (!this.createdAt) {
        this.createdAt = new Date();
    }
    next();
});

// 指定collection name，默认是复数
var ware = mongodb.model('ware', ModelSchema, 'ware');
exports = module.exports = ware;